#!/usr/bin/perl -w
package MobileOperatorPl;

# Moduł sprawdzający do jakiej sieci należy podany numer.
# Wymaga: perl + LWP (perl-libwww).
#
# (c) 2008 Dawid Cieszyński <cieszak..cieszak.eu>; http://cieszak.eu

use strict;
use HTTP::Request::Common;
use LWP::UserAgent;

#Sub: new
#	Constructor 
sub new {
	my ($class) = @_;

	my $self = {
		_verbose    => 0,
		_ua         => new LWP::UserAgent,
	};

	#creating user agent
	$self->{_ua}->timeout(30);
	$self->{_ua}->agent("Mozilla/5.0 Galeon/1.2.7 (X11; Linux i686; U;) Gecko/20021216");
	$self->{_ua}->env_proxy();

	bless $self, $class;
	return $self;
}

sub check {
	my $self = shift;
	my $number = shift;
	my $res = $self->{_ua}->request( GET "http://download.t-mobile.pl/updir/updir.cgi?msisdn=48".$number );
	
	my $result = "";

	if($res->content =~ /<tr><td><b>Operator:<\/b><\/td><td>([a-zA-Z0-9]*)<\/td>/)
	{
		#print $1."\n";
		$result = $1;
	}
	else
	{
		print "Error in returned data\n";
	}
	
	$result;
}

sub set_verbose {
	my ( $self, $verbose ) = @_;
	$self->{_verbose} = $verbose if defined($verbose);
	return $self->{_verbose};
}

#### DEBUG FUNCTIONS ####

#Sub: notice
#	Shows notice info if $verbose>0, else do nothing
sub notice {
	my $self = shift;
	if ( $self->{_verbose} > 0 ) {
		print "[N: ", shift, "]\n";
	}
}

#Sub: debug
#	Shows debug info if $verbose>1, else do nothing
sub debug {
	my $self = shift;

	if ( $self->{_verbose} > 1 ) {
		print "[D: ", shift, "]\n";
	}
}

#Sub: write_file
#   Write data to file if $verbose > 2, else do nothing
sub write_file {
	my $self = shift;

	if ( $self->{_verbose} > 2 ) {
		my ( $f, @data ) = @_;
		@data = () unless @data;
		open F, ">./$f" or $self->debug("Can't open $f : $!");
		print F @data;
		close F;
	}
}

#sample usage
# my $check = new MobileOperatorPl();
# print $check->check($ARGV[0]);

1;

